package com.carltechnology.mahjonggamecaculation.presenter.impl;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.carltechnology.mahjonggamecaculation.R;
import com.carltechnology.mahjonggamecaculation.base.MyAbstractSubscriber;
import com.carltechnology.mahjonggamecaculation.base.MyApplication;
import com.carltechnology.mahjonggamecaculation.dao.DataManager;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.presenter.IMahjongChoosePresenter;
import com.carltechnology.mahjonggamecaculation.utils.IRuntimeDataSolution;
import com.carltechnology.mahjonggamecaculation.utils.Utils;
import com.carltechnology.mahjonggamecaculation.views.IMahjongChooseView;
import com.carltechnology.mahjonggamecaculation.views.impl.MahjongRunningGameActivity;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class MahjongChoosePresenter implements IMahjongChoosePresenter {
    private IMahjongChooseView chooseView;
    private ChooseRecyclerViewAdapter adapter;
    private MahjongChoosePresenter(IMahjongChooseView view) {
        chooseView = view;
        List<IGameData> list = new CopyOnWriteArrayList<>();
        adapter = new ChooseRecyclerViewAdapter(list);
        RecyclerView recyclerView = chooseView.getRecyclerView();
        recyclerView.setAdapter(adapter);
        //获得Runtime状态
        Utils.getInstance().getRuntimeDataHelper(new MyRuntimeSolution()).getRuntimeData();
    }

    public static IMahjongChoosePresenter newInstance(IMahjongChooseView view){
        return new MahjongChoosePresenter(view);
    }

    @Override
    public void showGameData() {
        adapter.clearUp();
        MyAbstractSubscriber<IGameData> subscriber = new MyAbstractSubscriber<IGameData>() {
            @Override
            public void onNext(IGameData gameData) {
                adapter.add(gameData);
                MyApplication.getPool().add(gameData.getId());
            }
        };
        DataManager.getInstance().getDataSourceHelper().getMahjongInfo(subscriber);
    }

    @Override
    public void toRuntimeActivity() {
        if (Utils.getInstance().getRuntimeDataHelper().isRuntime()){
            Intent intent = new Intent(chooseView.getSelf(),MahjongRunningGameActivity.class);
            chooseView.getSelf().startActivity(intent);
        }
    }


    class ChooseRecyclerViewAdapter
            extends RecyclerView.Adapter<ChooseRecyclerViewAdapter.ViewHolder>{
        private final List<IGameData> mValuesList;

        public ChooseRecyclerViewAdapter(List<IGameData> mValuesList) {
            this.mValuesList = mValuesList;
        }
        public void add(IGameData mode){
            mValuesList.add(mode);
            notifyDataSetChanged();
        }
        public void delete(int pos){
            mValuesList.remove(pos);
            notifyDataSetChanged();
        }
        public void clearUp(){
            mValuesList.clear();
            notifyDataSetChanged();
        }
        @Override
        public ChooseRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mahjongchoose_item_list_content,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ChooseRecyclerViewAdapter.ViewHolder holder, int position) {
            holder.mode = mValuesList.get(position);
            String [] names = holder.mode.getValues();
            holder.getTimeView().setText(holder.mode.getDate());
            holder.getPlayer1View().setText(names[0]);
            holder.getPlayer2View().setText(names[1]);
            holder.getPlayer3View().setText(names[2]);
            holder.getPlayer4View().setText(names[3]);
         //   holder.getLabelView().setText(String.valueOf(position+1));
            holder.selfView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyApplication.getPool().setGameMode(holder.mode);
                    chooseView.toDetailView();
                }
            });
        }

        @Override
        public int getItemCount() {
            return mValuesList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            View selfView;
            @InjectView(R.id.list_content_time)
            TextView timeView;
            @InjectView(R.id.list_content_player1)
            TextView player1View;
            @InjectView(R.id.list_content_player2)
            TextView player2View;
            @InjectView(R.id.list_content_player3)
            TextView player3View;
            @InjectView(R.id.list_content_player4)
            TextView player4View;
            public IGameData mode;
            public ViewHolder(View itemView) {
                super(itemView);
                selfView = itemView;
                ButterKnife.inject(this,itemView);
            }

//            public TextView getLabelView() {
//                return labelView;
//            }

            public TextView getTimeView() {
                return timeView;
            }

            public TextView getPlayer1View() {
                return player1View;
            }

            public TextView getPlayer2View() {
                return player2View;
            }

            public TextView getPlayer3View() {
                return player3View;
            }

            public TextView getPlayer4View() {
                return player4View;
            }

            @Override
            public String toString() {
                return "ViewHolder{" +
                     //   "labelView=" + labelView.getText() +
                        ", timeView=" + timeView.getText() +
                        ", player1View=" + player1View.getText() +
                        ", player2View=" + player2View.getText() +
                        ", player3View=" + player3View.getText() +
                        ", player4View=" + player4View.getText() +
                        ", mode=" + mode.toString() +
                        '}';
            }
        }
    }
    private class MyRuntimeSolution implements IRuntimeDataSolution{

        @Override
        public void doRuntimeSolution() {
            chooseView.getHandler().post(new Runnable() {
                @Override
                public void run() {
                    toRuntimeActivity();
                }
            });
        }
    }
}
